package ysq.xyg.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.dao.AddressDao;
import ysq.xyg.model.Address;
import ysq.xyg.service.AddressService;
import ysq.xyg.util.BaseUtil;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yu sheng qiang
 * @create 2017-04-19 14:11
 **/
@Service("addressService")
public class AddressServiceImpl extends BaseServiceImpl<Address> implements AddressService {
    @Resource
    private AddressDao addressDao;
    @Autowired
    public void setAddressDao(AddressDao addressDao) {
        this.baseDao = addressDao;
    }

    //分页查询
    @Override
    public PageInfo<Address> pageInfo(Address address, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        //通用Example查询
        Example example = new Example(Address.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(address.getPhone())){
            criteria.andLike("phone","%"+address.getPhone()+"%");
        }
        if(StringUtil.isNotEmpty(address.getName())){
            criteria.andLike("name","%"+address.getName()+"%");
        }
        if(StringUtil.isNotEmpty(address.getMemberId())){
            criteria.andEqualTo("memberId",address.getMemberId());
        }
        if(StringUtil.isNotEmpty(address.getId())){
            criteria.andLike("id","%"+address.getId()+"%");
        }

        List<Address> list=addressDao.selectByExample(example);
        return new PageInfo<Address>(list);
    }

    //条件查询
    @Override
    public List<Address> list(Address address) {

        Example example = new Example(Address.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(address.getPhone())){
            criteria.andLike("phone","%"+address.getPhone()+"%");
        }
        if(StringUtil.isNotEmpty(address.getName())){
            criteria.andLike("name","%"+address.getName()+"%");
        }
        if(StringUtil.isNotEmpty(address.getMemberId())){
            criteria.andEqualTo("memberId",address.getMemberId());
        }
        example.orderBy("type");
        List<Address> list=addressDao.selectByExample(example);
        return list;
    }

    @Override
    public int updateDefaultType(String id) {
        return addressDao.updateDefaultType(id);
    }
}
